<?php
/**
 * @package : MSTAT.
 * @class-name : GearmandWriteLog()
 * @author : stephen.mo <stephen.mo@achievo.com>
 * @date : 2010-03-25
 */

class GearmandWriteLog {

    static private function logFormat($type) {
        $format_log = array(
                            'video_access'=>array('datetime','client_id','member_id','event','client_ip','bitrate','country_code','gender','age','pid','pname','episode_id','episode_num','video_id','video_title','category_id','category_name','category_path','content_type','package_id','content_lang','live','package_version'),
                            'plan_subscription'=>array('datetime','member_id','country_code','gender','age','package_id','package_version','plan_status','plan_type','order_ref','order_plan_id'),
                            'fp_video_access'=>array('datetime','member_id','ui_lang','client_ip','country_code','url','video_id','video_title','video_lang','programme_id','category_id','category_path','online_datetime','video_type','count_type','user_agent'),
                            'fp_page_access'=>array('datetime','member_id','ui_lang','client_ip','country_code','url','page_type','step','unique_id'),
                            'sign_in_out'=>array('datetime','member_id','action_type','rand_code','client_ip','domain','expire','country_code','gender','age','member_type','lang'),
                      );
        return $format_log[$type];
    }

    static public function write($log_entity) {
        if (!isset($log_entity['file'])) return false;
        $data = array();
        $tmp_data = array();
        $log_format_arr = array();

        $file_name = $log_entity['file'];
        $type = $log_entity['type'];
	    $data = $log_entity['data'];
        $log_format_arr = self::logFormat($type);
        
        foreach($log_format_arr as $key=>$value) {
            $tmp_value = (isset($data[$value])) ? $data[$value] : null;
            $tmp_data[$value] = $tmp_value;
        }

        $workload_content = implode('|',$tmp_data)."\n";
        $result = file_put_contents($file_name,$workload_content,FILE_APPEND);
        return $result;
    }

}

?>
